﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using Company.Merchandising.BusinessLayer.DataTransferObjects;
using Company.Merchandising.ServiceLayer.EntityManagement;
using Telerik.Web.UI;
using WebApplication.Code;
using WebApplication.Controls.Security.Roles;

namespace WebApplication.Restricted.Security.Roles
{
	public partial class RolesManagementPage : EntityPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void RolesGridInsertCommand(object source, GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			RolesEditControl rolesControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as RolesEditControl;
			if (rolesControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			DTORole role = rolesControl.EditedRole;
			RoleManager rolesManager = new RoleManager(ModelEntities);
			try
			{
				rolesManager.AddRole(rolesControl.EditedRole, rolesControl.ChosenFaculties);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void RolesGridUpdateCommand(object source, GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedRoleId;
			if (!int.TryParse(editedItem["Id"].Text, out editedRoleId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			RolesEditControl rolesControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as RolesEditControl;
			if (rolesControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			DTORole role = rolesControl.EditedRole;
			role.Id = editedRoleId;
			RoleManager rolesManager = new RoleManager(ModelEntities);
			try
			{
				rolesManager.UpdateRole(role, rolesControl.ChosenFaculties);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}
		protected void RolesGrid_CancelCommand(object source, GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			RolesEditControl rolesControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as RolesEditControl;
			if (rolesControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			rolesControl.AvailableFaculties = null;
			rolesControl.ChosenFaculties = null;
		}

		private void AddErrorMessage(string errorMessage)
		{
			Label errorLabel = new Label();
			errorLabel.Text = errorMessage;
			errorLabel.ForeColor = Color.Red;

			RolesGrid.Controls.Add(errorLabel);
		}
	}
}
